﻿using Business;
using Entity;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace BookManageSys.Admin
{
    public partial class UserManage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            UsersBusiness ub = new UsersBusiness();
            DataTable dt = ub.GetAllData();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void BtnAdd_Click(object sender, EventArgs e)
        {
            UsersEntity ue = new UsersEntity();
            ue.Username = TxtUsername.Text;
            ue.Password = TxtPassword.Text;
            ue.Address = TxtAddress.Text;
            ue.Telephone = TxtTelephone.Text;

            UsersBusiness ub = new UsersBusiness();
            int res = ub.AddUser(ue);
            if (res > 0)
            {
                Response.Write("<script>alert('添加成功！')</script>");
                BindData();
            }
            else
            {
                Response.Write("<script>alert('添加失败！')</script>");
            }
        }

        protected void BtnUpdate_Click(object sender, EventArgs e)
        {
            if (TxtUserid.Text != "")
            {
                UsersEntity ue = new UsersEntity();
                ue.Userid = Convert.ToInt32(TxtUserid.Text);
                ue.Username = TxtUsername.Text;
                ue.Password = TxtPassword.Text;
                ue.Address = TxtAddress.Text;
                ue.Telephone = TxtTelephone.Text;

                UsersBusiness ub = new UsersBusiness();
                int res = ub.EditUser(ue);
                if (res > 0)
                {
                    Response.Write("<script>alert('更新成功！')</script>");
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('更新失败！')</script>");
                }

            }
            else
            {
                Response.Write("<script>alert('请选择需要修改的商品！')</script>");
            }
        }

        protected void BtnSearch_Click(object sender, EventArgs e)
        {
            UsersEntity ue = new UsersEntity();
            ue.Username = TxtSearch.Text;

            UsersBusiness ub = new UsersBusiness();
            DataTable dt = ub.SearchAllData(ue);

            GridView1.DataSource = dt;
            GridView1.DataBind();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "删除")
            {
                UsersEntity ue = new UsersEntity();
                ue.Userid = Convert.ToInt32(e.CommandArgument);

                UsersBusiness ub = new UsersBusiness();
                int res = ub.DeleteUser(ue);
                if (res > 0)
                {
                    Response.Write("<script>alert('删除成功！')</script>");
                    BindData();
                }
                else
                {
                    Response.Write("<script>alert('删除失败！')</script>");
                }
            }
        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectIndex = GridView1.SelectedIndex;
            if (selectIndex != -1)
            {
                TxtUserid.Text = GridView1.Rows[selectIndex].Cells[1].Text;
                TxtUsername.Text = GridView1.Rows[selectIndex].Cells[2].Text;
                TxtPassword.Text = GridView1.Rows[selectIndex].Cells[3].Text;
                TxtAddress.Text = GridView1.Rows[selectIndex].Cells[4].Text;
                TxtTelephone.Text = GridView1.Rows[selectIndex].Cells[5].Text;
            }
        }
    }
}